﻿#region Using

using System;
using System.Diagnostics;

using Common.Base;

using CommunicationLibrary.TransportData;

using ServerApplication.Interfaces;

#endregion

namespace ServerApplication.Networking
{
    public class OutConnectionLayer : ICoreToNetwork
    {
        private readonly IPlayerManager playerManager;

        public OutConnectionLayer(IPlayerManager manager)
        {
            this.playerManager = manager;
        }

        public void SendGameLostMessage(Identifiable looser, Identifiable receiver)
        {
            var client = this.playerManager.FindClient(receiver);

            lock (client)
            {
                try
                {
                    client.PlayerLost(looser);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("There was an Exception while sending GameLost! Probably a Disconnect by the client.");
                    Debug.WriteLine(e.Message);
                }
            }
        }

        public void SendGamePausedMessage(Identifiable playerToInform)
        {
            var client = this.playerManager.FindClient(playerToInform);

            lock (client)
            {
                try
                {
                    client.GamePaused();
                }
                catch (Exception e)
                {
                    Debug.WriteLine("There was an Exception while sending GamePaused! Probably a Disconnect by the client.");
                    Debug.WriteLine(e.Message);
                }
            }
        }

        public void SendGameReady(Identifiable playerToInform, GameState startingState)
        {
            var client = this.playerManager.FindClient(playerToInform);

            lock (client)
            {
                try
                {
                    client.GameReady(startingState, playerToInform.Identifier);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("There was an Exception while sending GameReady! Probably a Disconnect by the client.");
                    Debug.WriteLine(e.Message);
                }
            }
        }

        public void SendGameResumedMessage(Identifiable playerToInform)
        {
            var client = this.playerManager.FindClient(playerToInform);

            lock (client)
            {
                try
                {
                    client.GameResumed();
                }
                catch (Exception e)
                {
                    Debug.WriteLine("There was an Exception while sending GameResumed! Probably a Disconnect by the client.");
                    Debug.WriteLine(e.Message);
                }
            }
        }

        public void SendGameStarted(Identifiable playerToInform)
        {
            var client = this.playerManager.FindClient(playerToInform);

            lock (client)
            {
                try
                {
                    client.GameStarted();
                }
                catch (Exception e)
                {
                    Debug.WriteLine("There was an Exception while sending GameStarted! Probably a Disconnect by the client.");
                    Debug.WriteLine(e.Message);
                }
            }
        }

        public void SendGameState(GameState gameState, Identifiable playerToInform)
        {
            var client = this.playerManager.FindClient(playerToInform);

            lock (client)
            {
                try
                {
                    client.UpdateGameState(gameState);
                }
                catch (Exception e)
                {
                    Debug.WriteLine("There was an Exception while sending updated gamestate! Probably a Disconnect by the client.");
                    Debug.WriteLine(e.Message);
                }
            }
        }

        public void SendGameWonMessage(Identifiable playerToInform)
        {
            var client = this.playerManager.FindClient(playerToInform);

            lock (client)
            {
                try
                {
                    client.HasWon();
                }
                catch (Exception e)
                {
                    Debug.WriteLine("There was an Exception while sending GameWon! Probably a Disconnect by the client.");
                    Debug.WriteLine(e.Message);
                }
            }
        }
    }
}